package org.pp.controller;


import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.pp.core.PageBean;
import org.pp.core.Result;
import org.pp.miniapp.dto.OrderAdd;
import org.pp.miniapp.dto.OrderPage;
import org.pp.service.IPpOrderService;
import org.pp.vo.MiniappOrderDetailVo;
import org.pp.vo.MiniappOrderVo;
import org.pp.vo.MiniappPreOrderVo;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class PpOrderController {

    @Autowired
    IPpOrderService orderService;

    @PostMapping("/prepare")
    @Operation(summary = "预下单")
    public Result<MiniappPreOrderVo>  prepare(@RequestBody List<Long> cartIds){
        MiniappPreOrderVo vo= orderService.prepare(cartIds);
        return Result.ok(vo);
    }

    @PostMapping("/create")
    @Operation(summary = "下单")
    public Result<String>  order(@RequestBody OrderAdd dto){
        String orderNo= orderService.create(dto);
        return Result.ok(orderNo);
    }


    @GetMapping("/list")
    @Operation(summary = "分页查询订单列表")
    public Result<PageBean<MiniappOrderVo>>  pageList(@ParameterObject OrderPage dto){
        PageBean<MiniappOrderVo> list=  orderService.pageList(dto);
        return Result.ok(list);
    }

    @PostMapping("/cancal")
    @Operation(summary = "取消订单")
    public Result<Void>  cancal(@RequestParam(required = true,name = "orderNo") String orderNo){
        orderService.cancal(orderNo);
        return Result.ok();
    }

    @PostMapping("/detail")
    @Operation(summary = "订单详情")
    public Result<MiniappOrderDetailVo>  detail(@RequestParam(required = true,name = "orderNo") String orderNo){
        MiniappOrderDetailVo vo=orderService.detail(orderNo);
        return Result.ok(vo);
    }


    @PostMapping("/reorder")
    @Operation(summary = "根据订单重新生成购物车项")
    public Result<List<Long>>  reorder(@RequestParam(required = true,name = "orderNo") String orderNo){
        List<Long> cartIds=orderService.reorder(orderNo);
        return Result.ok(cartIds);
    }

}
